package com.example.administrator.maizuofilm.notshow.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.maizuofilm.R;
import com.example.administrator.maizuofilm.bean.NotShow;
import com.example.administrator.maizuofilm.notshow.NotShowFragment;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by Administrator on 2016/7/12.
 */
public class NotShowAdapter extends RecyclerView.Adapter<NotShowAdapter.NotShowViewHolder> {
    public interface NotShowListener {
        void onLayoutClick(View v, int position);

        void onMessageClick(View v, int position);
    }

    private NotShowListener notShowListener;

    public void setNotShowListener(NotShowListener notShowListener) {
        this.notShowListener = notShowListener;
    }

    private List<NotShow.DataBean.FilmListBean> filmListBeen;
    private static final int INFO = 1;
    private static final int UNINFO = 2;
    private int type;
    private Context context;
    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

    public NotShowAdapter(List<NotShow.DataBean.FilmListBean> filmListBeen, Context context) {
        this.filmListBeen = filmListBeen;
        this.context = context;
    }

    @Override
    public NotShowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        NotShowViewHolder holder = null;
        switch (viewType) {
            case INFO:
                holder = new NotShowViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_message_notshow, null));
                type = INFO;
                break;
            case UNINFO:
                holder = new NotShowViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_less_notshow, null));
                type = UNINFO;
                break;
        }
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        if (filmListBeen.get(position).getInformation() == null) {
            return UNINFO;
        } else {
            return INFO;
        }
    }

    @Override
    public void onBindViewHolder(NotShowViewHolder holder, int position) {
        String date = format.format(Long.valueOf(filmListBeen.get(position).getFirstPlayTime()));
        holder.tv_title.setText(filmListBeen.get(position).getFilmName());
        holder.tv_info.setText(filmListBeen.get(position).getShortIntro());
        holder.tv_startdate.setText(date);
        if (holder.tv_details != null) {
            holder.tv_details.setText(filmListBeen.get(position).getInformation().getTitle());
        }
        Glide.with(context).load(filmListBeen.get(position).getMinposterAddress()).into(holder.imageView);

    }


    @Override
    public int getItemCount() {
        return filmListBeen.size();
    }


    class NotShowViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout relativeLayout;
        ImageView imageView;
        TextView tv_title, tv_info, tv_startdate, tv_message, tv_details;
        LinearLayout linearLayout;

        public NotShowViewHolder(View itemView) {
            super(itemView);
            relativeLayout = (RelativeLayout) itemView.findViewById(R.id.layout_notshow);
            tv_title = (TextView) itemView.findViewById(R.id.textview_title_less_notshow);
            tv_info = (TextView) itemView.findViewById(R.id.textview_info_less_notshow);
            tv_startdate = (TextView) itemView.findViewById(R.id.textview_startdate_less_notshow);
            tv_message = (TextView) itemView.findViewById(R.id.textview_message_notshow);
            tv_details = (TextView) itemView.findViewById(R.id.textview_details_notshow);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.layout_message_notshow);
            imageView = (ImageView) itemView.findViewById(R.id.imageview_notshow);
            relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (notShowListener != null) {
                        notShowListener.onLayoutClick(v, getLayoutPosition());
                    }
                }
            });
            if (linearLayout != null) {
                linearLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (notShowListener != null) {
                            notShowListener.onMessageClick(v, getLayoutPosition());
                        }
                    }
                });
            }
        }
    }
}
